﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using Entidades;
/*
 * Oct 9 2013
 * se creo la clase  MySqlTarifaDAO para manejar todo lo relacionado con la gestion de tarifas
 
 * **************************************************************************************************
 */
namespace Datos
{
    public class MySqlTarifaDAO:ITarifa
    {
        MySqlConnection conector = new MySqlConnection();
        string connectionString;
        public string s;

        public void iniciarConexion()
        {
            try
            {
                connectionString = "Server=127.0.0.1;Database=justime;Uid=root; pwd=root;";
                conector.ConnectionString = connectionString;
                conector.Open();
                s = "La conexion se ha realizado con exito";

            }
            catch
            {
                s = "Ocurrio un error al intentar conectarse";
            }

        }

        public void Conectar()
        {
            iniciarConexion();
        }

        public void Desconectar()
        {
            conector.Close();
        }

        public void Insertar(Entidades.Tarifa u)
        {
            MySqlCommand cmd = conector.CreateCommand();
            cmd.CommandText = "insert into Tarifa Values(?Distancia,?Precio)";
            cmd.Parameters.AddWithValue("?Distancia", u.Distancia);
            cmd.Parameters.AddWithValue("?Precio", u.Precio);
            s = cmd.CommandText;
            cmd.ExecuteNonQuery();
        }

        public List<Entidades.Tarifa> getTodos()
        {
            List<Tarifa> lst = new List<Tarifa>();
            MySqlCommand cmd = conector.CreateCommand();
            cmd.CommandText = "Select * From Tarifa";
            MySqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                Tarifa c = new Tarifa();
                c.Distancia = reader["Distancia"].ToString();
                c.Precio = reader["Precio"].ToString();
                lst.Add(c);
            }
            return lst;
        }

        public void actualizar(Tarifa u)
        {
            MySqlCommand cmd = conector.CreateCommand();
            cmd.CommandText = "UPDATE Tarifa SET Precio=?Precio WHERE Distancia=?Distancia;";
            cmd.Parameters.AddWithValue("?Distancia", u.Distancia);
            cmd.Parameters.AddWithValue("?Precio", u.Precio);
            cmd.ExecuteNonQuery();
        }
    }
}
